home *** CD-ROM | disk | FTP | other *** search
- unit ShFileOp;
- {Component based on the ShFileOperation API function
- Michel BURDIN - 1997}
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ShellApi;
-
- type
- TShFlags = (shAllowUndo, shFilesOnly, shNoConfirmation, shNoConfirmMkDir,
- shRenameOnCollision, shSilent, shSimpleProgress);
- TShFlag = Set of TShFlags;
- TShOp = (shCopy, shDelete, shMove, shRename);
- TShFileOP = class(TComponent)
- private
- fHParent: THandle;
- fOperation: TShOp;
- fTo: AnsiString;
- fFlags: TShFlag;
- fFromList: TStringList;
- fAborted: boolean;
- fTitle: AnsiString;
- fKeepFilesList: boolean;
- procedure SetFilesList(aList: TStringList);
- protected
-
- public
- constructor Create(aOwner: TComponent); override;
- destructor Destroy; override;
- function Execute: boolean;
- property Aborted: boolean read fAborted;
- published
- property Operation: TShOp read fOperation write fOperation default shCopy;
- property FilesList: TStringList read fFromList write SetFilesList;
- property Destination: AnsiString read fTo write fTo;
- property Title: AnsiString read fTitle write fTitle;
- property Options: TShFlag read fFlags write fFlags default [shAllowUndo];
- property KeepFilesList: boolean read fKeepFilesList write fKeepFilesList
- default False;
- end;
-
- procedure Register;
-
- implementation
-
- procedure Register;
- begin
- RegisterComponents('Samples', [TShFileOp]);
- end;
-
- constructor TShFileOp.Create(aOwner: TComponent);
- begin
- inherited Create(aOwner);
- if (aOwner is TWinControl) then fHParent := TWinControl(aOwner).Handle
- else fHParent := 0;
- fOperation := shCopy;
- fTo := '';
- fFlags := [shAllowUndo];
- fFromList := TStringList.Create;
- fAborted := False;
- fTitle := '';
- fKeepFilesList := False;
- end;
- destructor TShFileOp.Destroy;
- begin
- fFromList.Free;
- inherited Destroy;
- end;
- function TShFileOp.Execute: boolean;
- var
- TmpBuf: AnsiString;
- shFO: TSHFileOpStruct;
- i: Integer;
- begin
- Result := True;
- TmpBuf := '';
- {Get source files}
- if fFromList.Count > 0 then
- {all filenames are put together in an unique buffer,
- separated by a ; character + one at the end}
- for i := 0 to fFromList.Count-1 do
- if fFromList[i] <> '' then TmpBuf := TmpBuf + fFromList[i] + ';';
- {No source files, bye bye!}
- if TmpBuf = '' then exit;
- {before using the buffer each ; must be replaced by a
- null character. Therefore the buffer will end with
- 2 nulls (it's important)}
- for i := 1 to Length(TmpBuf) do
- if TmpBuf[i] = ';' then TmpBuf[i] := #0;
- {PREPARE SHFILEOPSTRUCT STRUCTURE}
- {--------------------------------------------------}
- {The Hwnd of the parent window is necessary, otherwise
- the task would be independant}
- shFO.Wnd := fHParent;
- {The operation to perform}
- case fOperation of
- shCopy: shFO.wFunc := FO_COPY;
- shDelete: shFo.wFunc := FO_DELETE;
- shMove: shFo.wFunc := FO_MOVE;
- shRename: shFo.wFunc := FO_RENAME;
- end;
- {List of source files}
- shFO.pFrom := PAnsiChar(TmpBuf);
- {destination : a directory or a filename}
- shFO.pTo := PAnsiChar(fTo);
- {some flags, of course}
- shFO.fFlags := 0;
- if shAllowUndo in fFlags then
- shFO.fFlags := shFO.fFlags or FOF_ALLOWUNDO;
- if shFilesOnly in fFlags then
- shFO.fFlags := shFO.fFlags or FOF_FILESONLY;
- if shNoConfirmation in fFlags then
- shFO.fFlags := shFO.fFlags or FOF_NOCONFIRMATION;
- if shNoConfirmMkDir in fFlags then
- shFO.fFlags := shFO.fFlags or FOF_NOCONFIRMMKDIR;
- if shRenameOnCollision in fFlags then
- shFO.fFlags := shFO.fFlags or FOF_RENAMEONCOLLISION;
- if shSilent in fFlags then
- shFO.fFlags := shFO.fFlags or FOF_SILENT;
- if shSimpleProgress in fFlags then
- shFO.fFlags := shFO.fFlags or FOF_SIMPLEPROGRESS;
- ShFO.fAnyOperationsAborted := False;
- ShFO.hNameMappings := nil; {not used, did not understand}
- shFO.lpszProgressTitle := pAnsiChar(fTitle);
- {Calling the following function will perform the action
- It return 0 if everything is OK or <> 0 if an error occured}
- Result := (ShFileOperation(shFO) = 0);
- {Aborted indicates if the user cancelled the operation}
- fAborted := ShFO.fAnyOperationsAborted;
- {Clear the list for the next operation, except if
- KeepFilesList is true}
- if not fKeepFilesList then fFromList.Clear;
- end;
- procedure TShFileOp.SetFilesList(aList: TStringList);
- begin
- {to avoid errors in design mode}
- fFromList.Assign(aList);
- end;
- {THAT'S ALL, FOLKS!}
- end.
-